#!/usr/bin/env python
#coding=utf-8 

import os
for tmpdir in ('/tmp', r'c:\tmp'):
	if os.path.isdir(tmpdir):
		break
	else:
		print 'no temp directory available'
		tmpdir = ''

if tmpdir:
	os.chdir(tmpdir)# 切换到新路径
	cwd = os.getcwd()
	print '*** current temprary director ***'
	print cwd # /tmp

	print '*** creating example directory...'
	os.mkdir('example')
	os.chdir('example')
	print '*** new working directoruy:'
	cwd = os.getcwd()
	print cwd # /tmp/example
	print "*** original directory listing: "
	print os.listdir(cwd) # []  目录下的文件
	print '*** creating test file...'
	fobj = open('test', 'w')
	fobj.write('foo\n')
	fobj.write('bar\n')
	fobj.close()
	print "*** updated directory listing: "
	print os.listdir(cwd) # ['test']

	print '*** renaming "test" to "filetest.txt" '
	os.rename('test','filetest.txt')
	print '*** updated directory listing:'
	print os.listdir(cwd) # ['filetest.txt']

	path = os.path.join(cwd, os.listdir(cwd)[0])
	print '*** full file pathname'
	print path # /tmp/example/filetest.txt
	print '*** (pathname, basename) =='
	print os.path.split(path) # ('/tmp/example', 'filetest.txt')
	print '*** (filename, extension) =='
	print os.path.splitext(os.path.basename(path)) # ('filetest', '.txt')

	print '*** displaying file contents:'
	fobj = open(path)
	for eachLine in fobj:
		print eachLine,
	fobj.close()

	print '*** deleting test file'
	os.remove(path)
	print '*** updated directory listing:'
	print os.listdir(cwd)
	os.chdir(os.pardir)
	print '*** deleting test directory'
	os.rmdir('example')
	print '*** DONE'


